<?php

namespace App\Controller\Api\User;

use App\Constants\ErrorCode;
use App\Controller\Api\BaseController;
use App\Model\ChatUser;
use App\Model\User;
use App\Service\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * @Controller(prefix="/user/v1/auth")
 */
class AuthController extends BaseController
{


    /**
     * @Inject()
     * @var UserService
     */
    protected $userService;


    /**
     * 普通登录
     * @RequestMapping(path="login")
     */
    public function login()
    {

        //TODO  登录验证


        //TODO 验证成功 取出用户需要存贮在jwt中的数据
        $token = $this->userService->userLogin(1, '1', 'wechat');

        if (!$token) {
            return $this->response->returnFail(ErrorCode::LOGIN_FAIL_NULL);
        }

        //TODO 异步处理登录后的操作


        return $this->response->returnSuccess([
            'type' => 'Bearer',
            'token' => $token,
            'expires_in' => $this->guard()->getJwtManager()->getTtl()
        ]);

    }


    /**
     * 微信登录
     * @RequestMapping(path="wechatLogin")
     * @return ResponseInterface
     */
    public function wechatLogin()
    {



        $params = $this->request->post();

        $authCode = $params['authCode'];

        //TODO 前端去授权得到用户的临时authCode 传回后台 去微信获取用户的openID 或者 开放平台唯一id

        $openId = 'iasdkjasdkasdasddfagdfagrf';


        $userInfo  = User::where(['wechat_id'=>$openId])->first();

        if ($userInfo){
            $uid = $userInfo['id'];
        }else{
            $baseInfo = [
                'wechat_id' => $openId,
                'type'=>1,
                'create_time'=>time(),
                'last_login_time'=>time(),
                'num'=>User::makeNum(),
                'nickname'=>getRandomString(6),
            ];

            $uid = User::addRegisterUser($baseInfo);
        }


        return $this->userService->userLogin($uid, $params['platform']??1, 'wechat');



    }


    /**
     * 苹果登录
     * @RequestMapping(path="appleLogin")
     * @return ResponseInterface
     */
    public function appleLogin(){
        $params = $this->request->post();

        $this->validate($params,[
            'appleId'=>'required'
        ]);


        $appleId = $params['appleId'];





        $userInfo  = User::where(['apple_id'=>$appleId])->first();

        if ($userInfo){
            $uid = $userInfo['id'];
        }else{
            $baseInfo = [
                'apple_id' => $appleId,
                'type'=>1,
                'create_time'=>time(),
                'last_login_time'=>time(),
                'num'=>User::makeNum()
            ];
            $uid = User::addRegisterUser($baseInfo);


        }


        return $this->userService->userLogin($uid, $params['platform']??1, 'apple');
    }





    public function register()
    {

        $params = $this->request->post();

        $this->validate($params, [

        ]);


    }


}
